/*
 * File     : Region.java
 * Created  : 6 May 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.type;

import java.nio.ByteBuffer;

import com.googlecode.dni.internal.DniInternal;


/**
 * <p>
 *  Encapsulates a region of memory.
 * </p>
 *
 * @author Matthew Wilson
 */
public final class Region implements NativeObject
{

    private final long address;

    private final int size;


    /**
     * @param pointer
     *            the pointer to the first byte
     * @param size
     *            the size in bytes
     */
    public Region( final Pointer pointer, final int size )
    {
        if ( size < 0 )
        {
            throw new IllegalArgumentException();
        }

        this.address = pointer.address();
        this.size = size;
    }

    /**
     * @param pointer
     *            the pointer to the first byte
     * @param size
     *            the size in bytes
     */
    private Region( final long pointer, final int size )
    {
        if ( size < 0 )
        {
            throw new IllegalArgumentException();
        }

        this.address = pointer;
        this.size = size;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Pointer pointer()
    {
        return Pointer.fromAddress( this.address );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isSameObject( final NativeObject other )
    {
        return other instanceof Region && ( (Region) other ).address == this.address;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString()
    {
        return "0x" + Long.toHexString( this.address ) + "+0x" + Long.toHexString( this.size ); //$NON-NLS-1$
    }

    /**
     * <p>
     *  Obtains the address in memory.
     * </p>
     *
     * @return the address
     */
    public long getAddress()
    {
        return this.address;
    }

    /**
     * <p>
     *  Determines the size of the region.
     * </p>
     *
     * @return the size of the region in bytes
     */
    public int getSize()
    {
        return this.size;
    }

    /**
     * <p>
     *  Creates a new buffer from this region.
     * </p>
     * <p>
     *  No check is made to verify whether there is actually valid memory
     *  mapped at the address range.
     * </p>
     *
     * @return a buffer; endian is native
     */
    public ByteBuffer getBuffer()
    {
        if ( this.address == 0L )
        {
            throw new IllegalStateException();
        }

        return DniInternal.MEMORY_ACCESSOR.getBuffer( this.address, this.size );
    }

    /**
     * <p>
     *  Obtains the region of memory used by the given buffer.
     * </p>
     *
     * @param buffer
     *            the buffer
     *
     * @return the pointer to position zero in the given buffer
     *
     * @throws IllegalArgumentException
     *             if the buffer is not direct
     */
    public static Region fromBuffer( final ByteBuffer buffer )
    {
        return new Region( DniInternal.MEMORY_ACCESSOR.getBufferPointer( buffer ),
                           buffer.capacity() );
    }

}
